﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;
using System.Text.RegularExpressions;


public partial class Admin_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //calngaycapnhat.SelectedDate = DateTime.Today;
            Load_data();
        }

    }
    //===========================================================================
    protected void Load_data()
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection cn = new SqlConnection(con);
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        cn.Open();
        string str = "select * from DonHang";
        SqlCommand cmd = new SqlCommand(str, cn);
        cmd.CommandText = str;
        cmd.Connection = cn;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        cn.Close();
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }

    protected void rept_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string con = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        SqlConnection cn = new SqlConnection(con);
        cn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        cn.Open();
        if (e.CommandName == "Sua")
        {
            ((Label)e.Item.FindControl("lblmakh")).Visible = false;
            ((Label)e.Item.FindControl("lblngaydathang")).Visible = false;
            ((Label)e.Item.FindControl("lblngaygiaohang")).Visible = false;
            ((Label)e.Item.FindControl("lbltennguoinhan")).Visible = false;
            ((Label)e.Item.FindControl("lbldiachinhan")).Visible = false;
            ((Label)e.Item.FindControl("lbldienthoainhan")).Visible = false;
            ((Label)e.Item.FindControl("lblhttt")).Visible = false;
            ((Label)e.Item.FindControl("lblhtgh")).Visible = false;
            ((Label)e.Item.FindControl("lbltrigia")).Visible = false;
            ((Label)e.Item.FindControl("lbldagiao")).Visible = false;
            ((TextBox)e.Item.FindControl("txtmakh")).Visible = true;
            ((TextBox)e.Item.FindControl("txtngaydathang")).Visible = true;
            ((TextBox)e.Item.FindControl("txtngaygiaohang")).Visible = true;
            ((TextBox)e.Item.FindControl("txttennguoinhan")).Visible = true;
            ((TextBox)e.Item.FindControl("txtdiachinhan")).Visible = true;
            ((TextBox)e.Item.FindControl("txtdienthoainhan")).Visible = true;
            ((TextBox)e.Item.FindControl("txthttt")).Visible = true;
            ((TextBox)e.Item.FindControl("txthtgh")).Visible = true;
            ((TextBox)e.Item.FindControl("txttrigia")).Visible = true;
            ((TextBox)e.Item.FindControl("txtdagiao")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkSua")).Visible = false;
            ((LinkButton)e.Item.FindControl("Button1")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkLuu")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkDung")).Visible = true;
        }
        if (e.CommandName == "Luu")
        {
            string makh = ((TextBox)e.Item.FindControl("txtmakh")).Text;
            string ngaydathang = ((TextBox)e.Item.FindControl("txtngaydathang")).Text;
            string ngaygiaohang = ((TextBox)e.Item.FindControl("txtngaygiaohang")).Text;
            string tennguoinhan = ((TextBox)e.Item.FindControl("txttennguoinhan")).Text;
            string diachinhan = ((TextBox)e.Item.FindControl("txtdiachinhan")).Text;
            string dienthoainhan = ((TextBox)e.Item.FindControl("txtdienthoainhan")).Text;
            string httt = ((TextBox)e.Item.FindControl("txthttt")).Text;
            string htgh = ((TextBox)e.Item.FindControl("txthtgh")).Text;
            string trigia = ((TextBox)e.Item.FindControl("txttrigia")).Text;
            string dagiao = ((TextBox)e.Item.FindControl("txtdagiao")).Text;
            string sql = "Update DonHang set makh= @makh, ngaydathang=@ngaydathang, ngaygiaohang=@ngaygiaohang, tennguoinhan=@tennguoinhan, diachinhan=@diachinhan ,dienthoainhan=@dienthoainhan, httt=@httt, htgh=@htgh, trigia=@trigia, dagiao=@dagiao where madh=@madh";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandText = sql;
            cmd.Connection = cn;
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            //SqlDataAdapter adp = new SqlDataAdapter("Update NhanVien set TenNhanVien= @TenNhanVien, NgaySinh=@NgaySinh,Email=@Email,DienThoai=@DienThoai ,Password=@Password where IDNhanVien = @IDNhanVien", cn);
            adp.SelectCommand.Parameters.AddWithValue("@makh", makh);
            adp.SelectCommand.Parameters.AddWithValue("@ngaydathang", ngaydathang);
            adp.SelectCommand.Parameters.AddWithValue("@ngaygiaohang", ngaygiaohang);
            adp.SelectCommand.Parameters.AddWithValue("@tennguoinhan", tennguoinhan);
            adp.SelectCommand.Parameters.AddWithValue("@diachinhan", diachinhan);
            adp.SelectCommand.Parameters.AddWithValue("@dienthoainhan", dienthoainhan);
            adp.SelectCommand.Parameters.AddWithValue("@httt", httt);
            adp.SelectCommand.Parameters.AddWithValue("@htgh", htgh);
            adp.SelectCommand.Parameters.AddWithValue("@trigia", trigia);
            adp.SelectCommand.Parameters.AddWithValue("@dagiao", dagiao);
            adp.SelectCommand.Parameters.AddWithValue("@madh", e.CommandArgument);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            Load_data();
        }
        if (e.CommandName == "Dung")
        {
            ((Label)e.Item.FindControl("lblmakh")).Visible = true;
            ((Label)e.Item.FindControl("lblngaydathang")).Visible = true;
            ((Label)e.Item.FindControl("lblngaygiaohang")).Visible = true;
            ((Label)e.Item.FindControl("lbltennguoinhan")).Visible = true;
            ((Label)e.Item.FindControl("lbldiachinhan")).Visible = true;
            ((Label)e.Item.FindControl("lbldienthoainhan")).Visible = true;
            ((Label)e.Item.FindControl("lblhttt")).Visible = true;
            ((Label)e.Item.FindControl("lblhtgh")).Visible = true;
            ((Label)e.Item.FindControl("lbltrigia")).Visible = true;
            ((Label)e.Item.FindControl("lbldagiao")).Visible = true;
            ((TextBox)e.Item.FindControl("txtmakh")).Visible = false;
            ((TextBox)e.Item.FindControl("txtngaydathang")).Visible = false;
            ((TextBox)e.Item.FindControl("txtngaygiaohang")).Visible = false;
            ((TextBox)e.Item.FindControl("txttennguoinhan")).Visible = false;
            ((TextBox)e.Item.FindControl("txtdiachinhan")).Visible = false;
            ((TextBox)e.Item.FindControl("txtdienthoainhan")).Visible = false;
            ((TextBox)e.Item.FindControl("txthttt")).Visible = false;
            ((TextBox)e.Item.FindControl("txthtgh")).Visible = false;
            ((TextBox)e.Item.FindControl("txttrigia")).Visible = false;
            ((TextBox)e.Item.FindControl("txtdagiao")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkSua")).Visible = true;
            ((LinkButton)e.Item.FindControl("Button1")).Visible = true;
            ((LinkButton)e.Item.FindControl("LinkLuu")).Visible = false;
            ((LinkButton)e.Item.FindControl("LinkDung")).Visible = false;
        }
        if (e.CommandName == "Xoa")
        {
            if (cn.State == ConnectionState.Closed)
            {
                cn.Open();
            }
            string sql = "delete from DonHang where madh=@madh";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.Parameters.AddWithValue("@madh", e.CommandArgument);// dung hàm  CommandArgument moi lay dc con truyen la k lay dc
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            Load_data();
        }
        cn.Close();
    }
}